<template>
<div>
  <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">

    <el-form-item label="ID" prop="ID">
      <el-input v-model="ruleForm.id"></el-input>
    </el-form-item>

    <el-form-item label="用户名" prop="username">
      <el-input v-model="ruleForm.username"></el-input>
    </el-form-item>

    <el-form-item label="昵称" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>

    <el-form-item label="性别" prop="gender">
      <el-select v-model="ruleForm.gender" placeholder="请选择">
        <el-option
            v-for="item in genderArr"
            :key="item.id"
            :label="item.gender"
            :value="item.gender">
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="手机号码" prop="phone">
      <el-input v-model="ruleForm.phone"></el-input>
    </el-form-item>


    <el-form-item label="电子邮箱" prop="email">
      <el-input v-model="ruleForm.email" placeholder="暂无"></el-input>
    </el-form-item>

    <el-form-item label="简介" prop="intro">
      <el-input v-model="ruleForm.intro" placeholder="暂无"></el-input>
    </el-form-item>

    <el-form-item label="头像">
      <!--limit控制图片数量-->
      <el-upload
          name="pic"
          action="http://localhost:8091/userupload"
          list-type="picture-card"
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog v-if="isShow" :visible.sync="dialogVisible">
        <img width="100%" :src="ruleForm.photo" alt="">
      </el-dialog>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
export default {
  data() {
    return {
      isShow:true,
      id:"",
      dialogVisible: false,
      genderArr:[{gender:"男"},{gender:"女"}],
      ruleForm: {}
    };
  },
  methods: {
    handleSuccess(response,file,fileList){
      this.ruleForm.photo = response;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.ruleForm.photo = file.photo;
      this.dialogVisible = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8091/user/update';
          console.log('url = ' + url);
          this.ruleForm.photo = "http://localhost:8091/user" + this.ruleForm.photo;
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData：' + formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response)=> {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '修改成功！',
                type: 'success'
              });

              this.resetForm(formName);
              location.reload();
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadUserInfo(){
      console.log("进入此方法");
      let jwt = localStorage.getItem('jwt');
      console.log(jwt);
      if (!jwt) {
        this.$message.error("未登录！")
        this.$router.push("/login");
      }
      const decode = this.jwt_decode(jwt);
      console.log(decode.id);
      this.id=decode.id;
      let url = "http://localhost:8091/user/"+this.id;
      console.log(url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) =>{
        console.log(response);
        let responseBody = response.data;
        console.log(responseBody);
        this.ruleForm = responseBody.data;
        console.log("数据1111112222222")
        console.log(this.user)
      });
    },
  },
  mounted() {
    this.loadUserInfo();
  }
}
</script>

<style scoped>

</style>